/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: WaybillServiceImpl
 * Author:   李毅
 * Date:     2019/10/17 11:34
 * Description: 业务实现类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.ymtms.supplycar.service.impl;

import com.ymtms.supplycar.dao.WaybillMapper;
import com.ymtms.supplycar.exception.CarException;
import com.ymtms.supplycar.pojo.Waybill;
import com.ymtms.supplycar.service.WaybillService;
import com.ymtms.supplycar.vo.GridRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈业务实现类〉
 *
 * @author 李毅
 * @create 2019/10/17
 * @since 1.0.0
 */
@Service
public class WaybillServiceImpl implements WaybillService {

    @Autowired
    private WaybillMapper waybillMapper;

    @Override
    public void insertWaybill(Waybill waybill) {
        int row = waybillMapper.insert(waybill);
        if (row == 0){
            throw new CarException("插入失败,请重试！");
        }
    }


    @Override
    public void deleteWaybillByWid(Integer id) {
        int row = waybillMapper.updateFlag(id);
        if (row == 0){
            throw new CarException("删除失败，请重试！");
        }
    }

    @Override
    public void updateWaybillByWid(Waybill waybill) {
        System.out.println(waybill);
        int row = waybillMapper.updateByPrimaryKey(waybill);
        if (row == 0){
            throw new CarException("修改失败，请重试！");
        }
    }

    @Override
    public List<Waybill> selectAll(GridRequest gridRequest,String contractCode,String date) {
        Integer begin = (gridRequest.getPage()-1) * gridRequest.getLimit();
        Integer end = gridRequest.getLimit();
        List<Waybill> waybills = waybillMapper.selectAll(begin,end,contractCode,date);
        if (waybills.isEmpty()){
            throw new CarException("查询失败，请重试！");
        }
        return waybills;
    }

    @Override
    public Integer selectTotalCount() {
        int count = waybillMapper.selectCount();
        if (count==0){
            throw new CarException("查询条数失败");
        }
        return count;
    }

    /**
     * @Description 根据货运单编号gmid查询相应的运输单
     * @Param gmid 货运单编号
     * @return Waybill 实体
     * @date   2019/11/6  10:09
     * @author 张泓
     */
    @Override
    public Waybill selectWaybillByGmid(Integer gmid) {
        return waybillMapper.selectWaybillByGmid(gmid);
    }


}